<template>
  <div class="workListItem">
    <div class="tableConten">
      <div class="itemTitle">
        <span class="redPoint">*</span>
        作业准备:
      </div>
      <div class="tebleDiv">
        <editTableNew
          :tableSetting="workListDetails"
          @handleClickAdd="addWorkList"
          :hideBtn="disabled"
          ref="workListTable"
        >
          <template #itemLable="{ editFormInfo }">
            <el-form :model="itemLable" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <template v-for="(item,index) in editFormInfo.itemLable">
                <el-form-item :label="item.name+'('+item.unit+'):'" prop="name" :key="index">
                  <el-input v-model="item.value" @change="val=>inputChange(val,item)"></el-input>
                </el-form-item>
              </template>
            </el-form>
          </template>
        </editTableNew>
      </div>
    </div>
    <div class="tableConten">
      <div class="itemTitle">
        <span class="redPoint">*</span>
        作业过程:
      </div>
      <div class="tebleDiv">
        <editTableNew :tableSetting="workProcessDetails" :hideBtn="disabled" ref="workProcessTable"></editTableNew>
      </div>
    </div>
  </div>
</template>
<script>
import { getAttachList } from "@/api/common";
export default {
  props: {
    formData: {
      type: Object,
      default: () => {
        return {};
      }
    },
    //是否可编辑
    disabled: {
      type: Boolean,
      default: false
    }
  },
  components: { },
  watch: {
    currentDept: {
      handler(val) {
        this.otherInfo.parentCode = val.id;
      },
      deep: true
    },
    formData: {
      handler(val) {
        this.attachSetting.businessId = val.id;
        let workListData = val.maintainReadyPoList || [];
        let workProcessData = val.maintainEquipmentPoList || [];
        this.workBookList = val.orderInstructionBeanList || []; /* 作业指导书 */
        if (!this.disabled) {
          workListData.map((i, index) => {
            if (index > 1) {
              i.editable = true;
            }
          });
          workProcessData.map(i => {
            i.editable = true;
          });
        } else {
          workListData.map((i, index) => {
            if (index > 1) {
              i.editable = false;
            }
          });
          workProcessData.map(i => {
            i.editable = false;
          });
        }
        this.$refs.workListTable.setTableData(workListData); //作业准备
        this.$refs.workProcessTable.setTableData(workProcessData); //作业过程
      },
      deep: true
    },
    disabled: {
      handler(val) {}
    }
  },
  data() {
    return {
      showBookDialog: false,
      tabActive: "111",
      collapseActive: "",
      workBookList: [],
      orgMemberSelect: {
        seleteType: "device",
        radio: true,
        props: {
          label: "name",
          children: "children"
        },
        column: [
          {
            label: "设备名称",
            prop: "name"
          }
        ],
        chosedData: []
      },
      otherInfo: {},
      showStandard: false,
      currentLineDevice: {},
      currentRow: null,
      typeId: null,

      hideBtn: false,
      workListData: [],
      itemLable: {},
      rules: [],
      workProcessData: [],
      /* 作业表单-作业准备 */
      workListDetails: {
        title: "",
        select: false,
        addMethons: true,
        maxHeight: "100%",
        btnArr: ["add"], //"edit",
        hideBtn: false,
        buttonArr: [
          {
            name: "删除",
            isShow: (row, index) => {
              if (index < 2) {
                return false;
              } else {
                return true;
              }
            },
            methods: (row, index) => {
              this.deletRow(row, index, "workList");
            }
          }
        ],
        data: [
          {
            type: "安全用具",
            tools: "安全帽、工作服、工作鞋"
          },
          {
            type: "工器具",
            tools: "工具袋、手电筒、梯子、清洁用具"
          }
        ],
        column: [
          {
            label: "类别",
            prop: "type",
            type: "text", //disableText
            required: true,
            width: 180
          },
          {
            label: "名称",
            prop: "tools",
            type: "text",
            autosize: true,
            required: true
          }
        ]
      },
      /* 作业表单-作业过程 */
      workProcessDetails: {
        btnArr: ["add"], //"edit",
        data: [],
        select: false,
        maxHeight: "100%",
        hideBtn: false,
        column: [
          {
            label: "设备",
            textAlign: "center",
            type: "device",
            prop: "equipmentName",
            idsStr: "equipmentId",
            required: true,
            readonly: true,
            methods: (data, row) => {
              row.type = data[0].typeId;
              this.typeId = data[0].typeId;
            }
          },
          {
            label: "作业标准",
            textAlign: "center",
            type: "click",
            icon: "el-icon-s-order",
            prop: "standardName",
            // required: true,
            readonly: true,
            method: (row, index) => {
              if (row.type) {
                this.currentRow = row;
                this.showStandard = true;
              } else {
                this.$message.warning("请先选择设备");
              }
            }
          }
        ],
        buttonArr: [
          {
            name: "删除",
            isShow: (row, index) => {
              return true;
            },
            methods: (row, index) => {
              this.deletRow(row, index, "process");
            }
          }
        ]
      },
      //附件设置
      attachSetting: {
        businessId: "", //单据的ID
        businessType: "workList"
      },
      majors: [
        {
          major: "111",
          majorName: "测试1",
          forms: [
            {
              formCode: "321",
              formName: "测试1-1",
              workReadys: [
                {
                  type: "安全用具",
                  tools: "安全帽、工作服、工作鞋"
                }
              ],
              orderItems: [{}]
            }
          ]
        },
        {
          major: "222",
          majorName: "测试2",
          forms: [
            {
              formCode: "123",
              formName: "测试2-1",
              workReadys: [
                {
                  type: "安全用具",
                  tools: "安全帽、工作服、工作鞋"
                }
              ],
              orderItems: [{}]
            },
            {
              formCode: "321",
              formName: "测试2-2",
              workReadys: [
                {
                  type: "安全用具",
                  tools: "安全帽、工作服、工作鞋"
                }
              ],
              orderItems: [{}]
            }
          ]
        }
      ]
    };
  },
  computed: {
    showBookList() {
      let bookLength = this.workBookList || [];
      return bookLength != 0;
    }
  },
  created() {},
  mounted() {
  },
  methods: {
    init() {
    },
    changeTableData(data) {
      data.map(i => {
        i.editable = true;
      });
    },
    //新增作业表单
    addWorkList(data, hadData) {
      let rowIndex = this.workListData.rowIndex;
      data.rowIndex = hadData.length + 1;
      // data.partSource = "spare_part";
      data.action = "删除";
      hadData.push(data);
      this.$refs.workListTable.setTableData(hadData);
    },
    handleDelete() {},
    inputChange(val, item) {},
    async getWorkListData() {
      let workListData = await this.$refs.workListTable.getTableData();
      workListData.map((i, index) => {
        delete i.action;
        i.sort = index;
      });
      return workListData;
    },
    async getworkProcessData() {
      let workProcessData = await this.$refs.workProcessTable.getTableData();
      workProcessData.map((i, index) => {
        delete i.isChange;
        delete i.type;
        i.sort = index;
      });
      return workProcessData;
    },
    getBookListData() {
      return this.workBookList;
    },
    //选择设备
    selectWorkStandard() {},
    //选择标准
    selectStandard() {
      let data = this.$refs.standardRelatedPage.getselectData();
      this.currentRow.standardName = data.name;
      this.currentRow.standardId = data.id;
      this.showStandard = false;
    },
    //选择作业知道书
    selectBook() {
      let data = this.$refs.bookList.getTableSelectData();
      let arr = [];
      data.map((i, index) => {
        let obj = {
          bussinessId: this.formData.id,
          instructionId: i.id,
          instructionName: i.name,
          orderType: "maintain",
          sort: index
        };
        arr.push(obj);
      });

      this.workBookList = arr;
      this.showBookDialog = false;
    },

    chooseBook() {
      this.showBookDialog = true;
    },
    /* 获取作业指导书 */
    getBookFile(item, type) {
      let form = {
        businessId: item.instructionId
      };
      getAttachList(form).then(res => {
        let data = res.data[0];
        if (res.data.length) {
          if (type == "preview") {
            this.preBook(data);
          } else if (type == "download") {
            this.downloadBook(data);
          }
        }
      });
    },

    //预览指导书
    preBook(item) {
      window.open(
        process.env.VUE_APP_BASEURL +
          "/eam-common-web/attachment/preview/?fileId=" +
          item.id
      );
      return true;
    },
    //下载指导书
    downloadBook(item) {
      this.$utils.downloadFile(item.id, item.name, item.path);
    },
    //删除指导书
    deletBook(item) {
      let list = this.workBookList;
      let arr = [];
      list.map(i => {
        if (i.id != item.id) {
          arr.push(i);
        }
      });
      this.workBookList = arr;
    },
    //删除表格的数据
    async deletRow(row, num, type) {
      let listArr = [];
      //获取所有填写的执行信息，不进行校验
      let data = [];
      if (type == "workList") {
        data = await this.$refs.workListTable.getTableData(false);
      } else if (type == "process") {
        data = await this.$refs.workProcessTable.getTableData(false);
      }
      data.map((i, index) => {
        if (index != num) {
          i.editable = true;
          listArr.push(i);
        }
      });
      if (type == "workList") {
        this.$refs.workListTable.setTableData(listArr);
      } else if (type == "process") {
        this.$refs.workProcessTable.setTableData(listArr);
      }
      //设置表格数据
    }
  }
};
</script>
<style lang="less">
.overhaul-workList {
}
</style>